home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / util / libs / graphics3d.lha / src / example / 3dlib.h next >
Encoding:
C/C++ Source or Header  |  1999-01-23  |  11.8 KB  |  457 lines

  1. /*******************************************************
  2.  ** Libreria di routin grafiche per la visualizzazione
  3.  ** in 3d .
  4.  ** Traslata in E V2.1b dalla libreria :
  5.  ** 2B3D Blitz Basic II 3D Graphics Engine Version 0.9 Beta
  6.  ** di Maciej R. Gorny
  7.  **
  8.  ** Autore del porting :
  9.  ** 1997 Patrizio Biancalani
  10.  ** NOTA: 
  11.  ** Nel porting tutte le variabili float sono state mutate
  12.  ** in variabili LONG intese pero' col formato 
  13.  ** a virgola fissa :val.float*FIXV
  14.  ** Questo per 2 motivi:
  15.  ** - E' piu' veloce anche su macchine senza coprocessore
  16.  ** - IL compilatore E V2.1B non tratta direttamente i numeri float
  17.  ** RICORDARSI SEMPRE :
  18.  ** in questa versione dell'E il simbolo
  19.  ** * significa prodotto tra 16bit*16bit=32bit
  20.  ** / significa divisione tra 32bit/16bit=16bit
  21.  ** quindi prestare MOLTA attenzione ai valori che possono
  22.  ** assumere in numeri in virgola fissa quando si usano 
  23.  ** questi valori
  24.  *******************************************************/
  25. #define div(a,b) Div(a,b)
  26. #define mul(a,b) Mul(a,b)
  27.  
  28. MODULE 'graphics','graphics/rastport','graphics/clip','layers'
  29. MODULE 'graphics/regions','graphics/gfx','graphics/layers'
  30. MODULE 'intuition/screens','dos/dos','Asl','libraries/Asl'
  31.  
  32. /** 
  33.    moduli per poter usare la nuova libreria graphics3D.library al posto della
  34.    gfx.h
  35. **/
  36. /**
  37.    modules to use the new library graphics3d.library instead of gfx.h
  38. **/
  39. MODULE 'graphics3D','graphics3D_lib'
  40.  
  41. /*** INIZIO CODICI ESEGUIBILI ***/
  42.  
  43. /*** ROUTIN DI USO ANCHE ESTERNO ***/
  44.  
  45. /************************************************
  46.  * apertura e inizializzazione dell'ambiente 3D * 
  47.  * restituisce puntatore ad area generale da    *
  48.  * indicare sempre al richiamo delle altre      *
  49.  * routin.                    *
  50.  ************************************************
  51.  *** INPUT :                    *
  52.  * win  -> puntatore a finestra su cui si vuol  *
  53.  *         operare.                *
  54.  * x0   -> coord. X origine finestra di vis.    *
  55.  * y0   -> coord. Y origine finestra di vis.    *
  56.  * scrw -> larghezza finestra per mondo 3D.     *
  57.  * scrh -> altezza finestra per mondo 3D.       *
  58.  * vdist-> distanza tra osservatore e piano di  *
  59.  *         proiezione.                    *
  60.  *** OUTPUT:                    *
  61.  * puntatore a struttura ambient3d da usarsi in *
  62.  * ingresso di quasi tutte le altre funzioni.   *
  63.  * > 0 allora tutto ok.                *
  64.  * =0 allora errore, operazione fallita.        * 
  65.  ************************************************/
  66. PROC display3d(win:PTR TO window,x0:LONG,y0:LONG,
  67.         scrw:LONG,scrh:LONG,vdist:LONG)
  68. DEF     ris:LONG 
  69.  
  70. ris:=NIL
  71. graphics3dbase:=NIL
  72. IF ((graphics3dbase:=OpenLibrary('graphics3D.library',11))<=NIL) THEN RETURN 0
  73.  
  74. ris:=Gd_display3d(win,x0,y0,scrw,scrh,vdist)
  75.  
  76. WriteF('ris=\d\n',ris)
  77.  
  78. ENDPROC ris
  79.  
  80. /************************************************
  81.  * chiusura ed eliminazione dell'ambiente 3D    * 
  82.  ************************************************
  83.  *** INPUT :                    *
  84.  * in -> valore >0 restituito da display3d.     *
  85.  *** OUTPUT:                    *
  86.  * nessuno.                    *
  87.  ************************************************/
  88. PROC close_display3d(in:LONG)
  89. DEF i:LONG
  90.  
  91. IF in<>NIL THEN Gd_close_display3d(in)
  92.  
  93. IF graphics3dbase<>NIL 
  94.     CloseLibrary(graphics3dbase)
  95.     RETURN
  96. ENDIF
  97.  
  98. ENDPROC
  99.  
  100. /************************************************
  101.  ** legge i dati di un oggetto da un file .PLG **
  102.  ** e li inserisce nel mondo 3d come un nuovo  **
  103.  ** oggetto.                       ** 
  104.  ************************************************
  105.  *** INPUT :                        * 
  106.  * in -> valore > 0 restituito da display3d.    *
  107.  * fname -> puntatore a stringa con nome file.  *
  108.  * scalar-> fattore di scala per oggetto.       *
  109.  *          (valore in FIXPOINT).        *
  110.  *** OUTPUT:                    *
  111.  * >0 tutto ok, oggetto letto.            *
  112.  * =0 operazione fallita.            *
  113.  ************************************************/
  114. PROC plgloadobject(in:LONG,fname:PTR TO CHAR,scalar:LONG)
  115. DEF total_vertices:LONG,
  116.     total_polys:LONG,
  117.     num_vertices:LONG,
  118.     color_des:LONG,
  119.     logical_color:LONG,
  120.     shading:LONG,
  121.     index:LONG,
  122.     ln:LONG,
  123.     tempword:LONG,
  124.     p1:LONG,
  125.     p2:LONG,
  126.     p3:LONG,
  127.     p4:LONG,    
  128.     tempbyte:LONG,
  129.     t:LONG,
  130.     k:LONG,
  131.     v:vertex,
  132.     vl:PTR TO LONG,
  133.     temp[500]:ARRAY,
  134.     sbuffer[100]:ARRAY,
  135.     name[50]:ARRAY,
  136.     tempstr[50]:ARRAY,
  137.     file:LONG,
  138.     col:LONG,
  139.     cl:LONG,
  140.     cc:LONG,
  141.     cs:LONG,
  142.     i:LONG,
  143.    esi:LONG
  144.  
  145. esi:=0
  146. file:=Open(fname,OLDFILE)
  147. IF file=NIL THEN RETURN 0
  148. ln:=0
  149. cs:=0
  150. WHILE ((cl:=Read(file,temp,495))>NIL)
  151.     cc:=0
  152.     WHILE (cc<cl)
  153.         sbuffer[cs++]:=temp[cc++]
  154.         IF (sbuffer[cs-1]=$0D) OR (sbuffer[cs-1]=$0A) 
  155.             sbuffer[cs]:=0
  156. /** mettere qui codici di lettura dati da file **/
  157. /** place here the code of reading datas from file **/
  158.     plggetline(sbuffer)
  159. #ifdef DEBUG
  160. WriteF('Line read by plggetline :\s\n',sbuffer)
  161. #endif
  162.     IF StrLen(sbuffer)>3   /* test se linea non vuota *//* test if line not empty */
  163.         INC ln
  164.         IF ln=1        
  165.             tempbyte:=lstr(sbuffer,name,0)        
  166.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  167.             total_vertices:=Val(tempstr,{i})
  168. #ifdef DEBUG
  169. WriteF('Total_vertices:\d\n',total_vertices)
  170. #endif
  171.             lstr(sbuffer,tempstr,tempbyte)
  172.             total_polys:=Val(tempstr,{i})
  173. #ifdef DEBUG
  174. WriteF('Total_polys:\d\n',total_polys)
  175. #endif
  176.             t:=Gd_newobj(in,name,total_polys,total_vertices)
  177. #ifdef DEBUG
  178. WriteF('Id. of new object:\d\n',t)
  179. #endif
  180.             IF t=NIL THEN JUMP abort
  181.         /** valori fix point **/
  182.         /** values in fix point **/
  183.         ELSEIF (ln>=2) AND ((total_vertices+1)>=ln)
  184.             tempbyte:=lstr(sbuffer,tempstr,0)
  185.             v.x:=Val(tempstr,{i})
  186.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  187.             v.y:=Val(tempstr,{i})
  188.             lstr(sbuffer,tempstr,tempbyte)
  189.             v.z:=Val(tempstr,{i})
  190. #ifdef DEBUG
  191. WriteF('Value of vertex n#\d in integer : x=\d y=\d y=\d\n',ln-2,v.x,v.y,v.z) 
  192. #endif
  193.             v.x:=mul(v.x,scalar)
  194.             v.y:=mul(v.y,scalar)
  195.             v.z:=mul(v.z,scalar)
  196. #ifdef DEBUG
  197. WriteF('Value of vertex n#\d in fixpoint : x=\d y=\d y=\d\n',ln-2,v.x,v.y,v.z) 
  198. WriteF('Result of GD_addobjvertex :\d\n',
  199.             Gd_addobjvertex(in,ln-2,v.x,v.y,v.z) )
  200. #endif
  201. #ifndef DEBUG
  202.             Gd_addobjvertex(in,ln-2,v.x,v.y,v.z)
  203. #endif
  204.         ELSEIF (total_vertices+2<=ln) AND 
  205.             (total_polys+total_vertices+1>=ln)
  206.             tempbyte:=lstr(sbuffer,tempstr,0)
  207.     /** implementati dec. hex($,0x) e binary(%) **/
  208.     /** implemented decimal hex($,0x) and bynary(%) **/
  209.             IF (tempstr[0]="0") AND (tempstr[1]="x") 
  210.                 tempstr[0]:=" "
  211.                 tempstr[1]:="$"
  212.             ENDIF
  213.             color_des:=Val(tempstr,{i})
  214. #ifdef DEBUG
  215. WriteF('Color descriptor (in hexadecimal):\h\n',color_des)
  216. #endif
  217.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  218.             tempword:=total_vertices+2
  219.             num_vertices:=Val(tempstr,{i})
  220.             index:=ln-tempword
  221. #ifdef DEBUG
  222. WriteF('Number of vertex in #\d polygons:\d\n',index,num_vertices)
  223. #endif            
  224.             p2:=-1
  225.             p3:=-1
  226.             p4:=-1    
  227.             IF num_vertices>0
  228.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  229.                 p1:=Val(tempstr,{i})
  230. #ifdef DEBUG
  231. WriteF('First vertex n#\d\n',p1)
  232. #endif
  233.             ENDIF
  234.             IF num_vertices>1
  235.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  236.                 p2:=Val(tempstr,{i})
  237. #ifdef DEBUG
  238. WriteF('Second vertex n#\d\n',p2)
  239. #endif
  240.             ENDIF
  241.             IF num_vertices>2
  242.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  243.                 p3:=Val(tempstr,{i})
  244. #ifdef DEBUG
  245. WriteF('Third vertex n#\d\n',p3)
  246. #endif
  247.             ENDIF
  248.             IF num_vertices>3
  249.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  250.                 p4:=Val(tempstr,{i})
  251. #ifdef DEBUG
  252. WriteF('Fourth vertex n#\d\n',p4)
  253. #endif
  254.             ENDIF
  255. #ifdef DEBUG
  256. WriteF('Result of GD_addobjpoly :\d\n',
  257.             Gd_addobjpoly(in,index,p1,p2,p3,p4) )
  258. #endif
  259. #ifndef DEBUG
  260.             Gd_addobjpoly(in,index,p1,p2,p3,p4)
  261. #endif
  262.             col:=And($FF,color_des)
  263.             IF col<16 THEN col:=1
  264.             IF col>16 THEN col:=16
  265. #ifdef DEBUG
  266. WriteF('Color :\d\n',col)
  267. #endif
  268.             Gd_cattpoly(in,index,col,Shr(color_des,12))
  269.         ENDIF
  270.     ENDIF
  271. /****************************************/
  272.             cs:=0
  273.         ENDIF
  274.     ENDWHILE
  275. ENDWHILE
  276. /** 
  277.     remeber that you must be use this function ever than the end of 
  278.     definition of a new object
  279. **/
  280. Gd_recalcobj(in)
  281. esi:=1
  282.  
  283. abort:
  284. Close(file)
  285.  
  286. ENDPROC esi
  287.  
  288. /*** ROUTIN DI USO SOLO INTERNO ***/
  289.  
  290. /******************************************************/
  291. /** elimina i commenti da una stringa di un file PLG **/
  292. /** erase the comment from a string of a PLG file    **/
  293. /******************************************************/
  294. PROC plggetline(strl:PTR TO CHAR)
  295. DEF i:LONG
  296. DEF a:LONG
  297. DEF f:LONG
  298. DEF lens:LONG
  299.  
  300. f:=0
  301. a:=0
  302. lens:=StrLen(strl)
  303. FOR i:=0 TO lens
  304.     IF (strl[i]<>32) THEN f:=1
  305.     IF f<>NIL
  306.         IF (strl[i]="#") OR (strl[i]=";") 
  307.             i:=lens
  308.         ELSE
  309.             strl[a]:=strl[i]
  310.             INC a
  311.         ENDIF
  312.     ENDIF
  313. ENDFOR
  314.  
  315. strl[a]:=0
  316.  
  317. ENDPROC
  318.  
  319. /*************************************************/
  320. /** in str mette la prima sequenza di caratteri **/
  321. /** diversi da spazio che trova in s a partire  **/
  322. /** da start, str deve essere lunga a sufficenza**/
  323. /** ritorno prima pos. dopo sequenza.        **/
  324. /** in 'str' place the first sequence of caracters **/
  325. /** other than space that are in 's' from 'start', **/
  326. /** 'str' must be sufficent long , result is the   **/
  327. /** first position after the sequence.             **/ 
  328. /*************************************************/
  329. PROC lstr(s:PTR TO CHAR,str:PTR TO CHAR,start:LONG)
  330. DEF esi:LONG
  331. DEF f:LONG
  332. DEF ls:LONG
  333. DEF esif:LONG
  334. esi:=0
  335. f:=start
  336. ls:=StrLen(s)
  337.  
  338. /** elimino eventuali spazi precedenti **/
  339. WHILE ((f<ls) AND ((s[f]=" ") OR (s[f]=$0A) OR (s[f]=$0D))) DO INC f
  340. /** calcolo lunghezza sequenza caratteri <> " " **/
  341. esif:=esi+f
  342. WHILE (((esif)<ls) AND (s[esif]<>" ") AND
  343.     (s[esif]<>$0A) AND (s[esif]<>$0D))
  344.     str[esi]:=s[esif] 
  345.     INC esi
  346.     INC esif
  347. ENDWHILE
  348. str[esi]:=0
  349.  
  350. #ifdef DEBUG
  351. WriteF('lstr : s=>\s< str=>\s< start=\d result=\d\n',s,str,start,esi+f)
  352. #endif
  353.  
  354. ENDPROC esi+f
  355.  
  356. /**** FINE ROUTIN ***/
  357.  
  358. /*** ROUTIN DI USO SOLO PER TEST **/
  359.  
  360.  
  361. /*************************************************/
  362. /** stampa contenuto delle strutture polytemp   **/
  363. /*************************************************/
  364. /*
  365. PROC printpolyt(in:LONG)
  366. DEF i:LONG
  367. DEF pt:PTR TO polytemp
  368.  
  369.  
  370. pt:=in.worldpolys
  371. FOR i:=0 TO in.total_polys
  372.     WriteF('pol#\d\n',i)
  373.     WriteF('seg#1 X=\d Y=\d\n',pt[i].x1,pt[i].y1)
  374.     WriteF('seg#2 X=\d Y=\d\n',pt[i].x2,pt[i].y2)
  375.     WriteF('seg#3 X=\d Y=\d\n',pt[i].x3,pt[i].y3)
  376.     WriteF('seg#4 X=\d Y=\d\n',pt[i].x4,pt[i].y4)
  377.     WriteF('seg#5 X=\d Y=\d\n',pt[i].x5,pt[i].y5)
  378.  
  379.     WriteF('n#segmenti=\d col=\d\n',pt[i].numpoints,pt[i].shade)
  380.  
  381.     WriteF('vmode=\d\n',pt[i].vmode)
  382.  
  383. ENDFOR
  384.  
  385. ENDPROC
  386. */
  387.  
  388. /*************************************************/
  389. /** stampa contenuto delle strutture objectnode **/
  390. /*************************************************/
  391. /*
  392. PROC objectprint(in:LONG)
  393. DEF id:LONG
  394. DEF io:PTR TO LONG
  395. DEF i:LONG
  396. DEF k:LONG
  397. DEF vo:PTR TO vertex
  398. DEF vl:PTR TO vertex
  399. DEF vg:PTR TO vertex
  400. DEF vc:PTR TO vertex
  401. DEF obj:PTR TO objectnode
  402. DEF pol:PTR TO polygon
  403.  
  404. WriteF('indici oggetti:\n')
  405. io:=in.iobjects
  406. FOR id:=0 TO in.total_objects-1
  407.     WriteF('#\d oggetto id:\d\n',id,io[id])
  408. ENDFOR
  409. FOR id:=0 TO in.total_objects-1
  410.     in.attuale:=id
  411.     obj:=pobj(in)
  412.     vo:=obj.vorig
  413.     vl:=obj.vlocal
  414.     vc:=obj.vcamera
  415.     pol:=obj.polys
  416.     WriteF('id:\d Name:\s\n',obj.id,obj.name)
  417.     WriteF('num. of vert.:\d\n',obj.numverts)
  418.     WriteF('num. of poly.:\d\n',obj.numpolys)
  419.     WriteF('view_mode:\d\n',obj.shade)
  420.     WriteF('state:\d\n',obj.state)
  421.     WriteF('world pos.: X=\d Y=\d Z=\d\n',
  422.         obj.worldposx/FIXV,obj.worldposy/FIXV,obj.worldposz/FIXV)
  423.     WriteF('bounding box:\n max x:\d y:\d z:\d\n min x:\d y:\d z:\d\n',
  424.         obj.xmax,obj.ymax,obj.zmax,obj.xmin,obj.ymin,obj.zmin)
  425.     WriteF('original vertices:\n')
  426.     FOR k:=0 TO obj.numverts-1
  427.         WriteF('\d \d \d\n',vl[k].x/FIXV,
  428.             vl[k].y/FIXV,vl[k].z/FIXV)
  429.     ENDFOR
  430.     WriteF('local vertices:\n')
  431.     FOR k:=0 TO obj.numverts-1
  432.         WriteF('\d \d \d\n',vl[k].x/FIXV,
  433.             vl[k].y/FIXV,vl[k].z/FIXV)
  434.     ENDFOR
  435.     WriteF('camera vertices:\n')
  436.     FOR k:=0 TO obj.numverts-1
  437.         WriteF('\d \d \d\n',vc[k].x/FIXV,
  438.             vc[k].y/FIXV,vc[k].z/FIXV)
  439.     ENDFOR
  440.     WriteF('poligon list:\n')
  441.     FOR i:=0 TO obj.numpolys-1
  442.         WriteF('poligon #\d\n',i)
  443.         WriteF('\d,',pol[i].vertexlist0)
  444.         WriteF('\d,',pol[i].vertexlist1)
  445.         WriteF('\d,',pol[i].vertexlist2)
  446.         WriteF('\d,',pol[i].vertexlist3)
  447.         WriteF('color:\d shade:\d 2-sided:\d\n',
  448.             pol[i].color,pol[i].shade,pol[i].twosided)
  449.         WriteF('visible:\d clipped:\d active:\d\n',
  450.             pol[i].visible,pol[i].clipped,pol[i].active)
  451.     ENDFOR    
  452. ENDFOR
  453.  
  454. ENDPROC
  455. */
  456.  
  457.